<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>joystick</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="jitter2.html" title="jitter2" />
    <link rel="next" href="jspline.html" title="jspline" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">joystick</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="jitter2.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="jspline.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="joystick"></a>
      <div class="titlepage"></div>
      <a id="Indexjoystick" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">joystick</span>
        </h2>
        <p>joystick — 
      Reads data from a joystick controller.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp118116288"></a>
        <h2>Description</h2>
        <p>
       Reads data from a Linux joystick controller
    </p>
      </div>
      <div class="refsect1">
        <a id="idp118117472"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">kres <span class="command"><strong>joystick</strong></span> kdevice ktab</pre>
      </div>
      <div class="refsect1">
        <a id="idp118192960"></a>
        <h2>Performance</h2>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">Note</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
       Please note that this opcode is currently only supported on
       Linux.
     </p>
              </td>
            </tr>
          </table>
        </div>
        <p>
     <span class="emphasis"><em>kdevice</em></span> -- the index of the joystick device, either /dev/js<span class="emphasis"><em>N</em></span> or
            /dev/input/js<span class="emphasis"><em>N</em></span>.
   </p>
        <p>
     <span class="emphasis"><em>ktab</em></span> -- A table to hold input results,
     should be at least enough elements to store one value for each
     stick axis and one for each button + 2.  The first two elements
     of the table are initialized with the number of axes and the
     number of buttons, respectively, when a joystick is opened. If a
     joystick is unplugged during performance, the opcode will
     repeatedly attempt to reopen the device with a delay between
     attempts.
   </p>
      </div>
      <div class="refsect1">
        <a id="idp118197536"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the joystick opcode. It uses the file <a class="ulink" href="examples/joystick.csd" target="_top"><em class="citetitle">joystick.csd</em></a>.
    </p>
        <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
        <p>
    </p>
        <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
-odac      <span class="comment">;;;realtime audio out</span>
<span class="comment">;-iadc    ;;;uncomment -iadc if realtime audio input is needed too</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o joystick.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">ksmps</span> <span class="op">=</span> 32
<span class="ohdr">nchnls</span> <span class="op">=</span> 2
<span class="comment">;0dbfs  = 1</span>

<span class="oblock">instr</span> 1 <span class="comment">; gives information about your joystick in real time</span>
  
kmask    joystick   0, 1
kidx     <span class="op">=</span>  2
kaxes    <span class="opc">tab</span> 0, 1 <span class="comment">; number of axes has been stored in position 0</span>
kbuttons <span class="opc">tab</span> 1, 1 <span class="comment">; number of buttons has been stored in position 1</span>
         <span class="opc">printf</span> "this joystick has <span class="op">%</span>d axes, <span class="op">%</span>d buttons\n", kidx, kaxes, kbuttons
kuniq    <span class="opc">init</span> 0

<span class="olabel">reportaxis</span><span class="op">:</span> <span class="comment">; first we see if we have any x/y input</span>
	kcheck    <span class="op">=</span>  kmask <span class="op">&amp;</span> (1<span class="op">&lt;</span><span class="op">&lt;</span>kidx)
	<span class="octrl">if</span> kcheck <span class="op">==</span> 0 <span class="octrl">kgoto</span> <span class="olabel">nexta</span>
	kres      <span class="opc">tab</span>       kidx, 1
	kuniq     <span class="op">=</span>  kuniq <span class="op">+</span> 1 <span class="comment">; to be sure to make the printf print</span>
		  <span class="opc">printf</span>    "axis <span class="op">%</span>d, value <span class="op">%</span>6d\n", kuniq, kidx<span class="op">-</span>2, kres
<span class="olabel">nexta</span><span class="op">:</span>
	kidx      <span class="op">=</span>  kidx<span class="op">+</span>1
	<span class="octrl">if</span> kidx <span class="op">&lt;</span> (kaxes<span class="op">+</span>2) <span class="octrl">kgoto</span> <span class="olabel">reportaxis</span>

<span class="olabel">reportbutton</span><span class="op">:</span> <span class="comment">; now we check for any buttons pressed</span>
	kcheck    <span class="op">=</span>  kmask <span class="op">&amp;</span> 1<span class="op">&lt;</span><span class="op">&lt;</span>kidx
	<span class="octrl">if</span> kcheck <span class="op">==</span> 0 <span class="octrl">kgoto</span> <span class="olabel">nextb</span>
	kres      <span class="opc">tab</span>       kidx, 1 <span class="comment">; a button has been pressed, get from table</span>
		  <span class="opc">printf</span>    "button <span class="op">%</span>d, pushed\n", kidx<span class="op">*</span>kres, (kidx<span class="op">-</span>(kaxes<span class="op">+</span>2))
		  <span class="opc">printf</span>    "button <span class="op">%</span>d, released\n", kidx<span class="op">*</span>(1<span class="op">-</span>kres), (kidx<span class="op">-</span>(kaxes<span class="op">+</span>2))

<span class="olabel">nextb</span><span class="op">:</span>
	kidx      <span class="op">=</span>  kidx<span class="op">+</span>1
	<span class="octrl">if</span> kidx <span class="op">&lt;</span> (kaxes<span class="op">+</span>kbuttons<span class="op">+</span>2) <span class="octrl">kgoto</span> <span class="olabel">reportbutton</span>

<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="stamnt">f</span>1  0 32    7     0     7     0         <span class="comment">; will hold the joystick data</span>

<span class="stamnt">i</span>1  0  60000

<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
        <p>
      Here is another example of the joystick opcode. It uses the file <a class="ulink" href="examples/joystick-2.csd" target="_top"><em class="citetitle">joystick-2.csd</em></a>.
    </p>
        <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
-odac    <span class="comment">;;;realtime audio out</span>
<span class="comment">;-iadc    ;;;uncomment -iadc if realtime audio input is needed too</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o joystick-2.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">ksmps</span> <span class="op">=</span> 32
<span class="ohdr">nchnls</span> <span class="op">=</span> 2
<span class="comment">;0dbfs  = 1</span>

<span class="oblock">instr</span> 1

kmask   joystick   0, 1
kaxes    <span class="opc">init</span> 0
kbuttons <span class="opc">init</span> 0
kx0      <span class="opc">init</span> 0 <span class="comment">; first two entries are # of axes and # of buttons, </span>
ky0      <span class="opc">init</span> 0 <span class="comment">; then axes, then buttons                           </span>
         <span class="opc">vtabk</span> 0, 1, kaxes, kbuttons, kx0, ky0
kidx     <span class="op">=</span>  2<span class="op">+</span>kaxes 
                       
<span class="olabel">buttons</span><span class="op">:</span>
  kcheck <span class="op">=</span>  kmask <span class="op">&amp;</span> 1<span class="op">&lt;</span><span class="op">&lt;</span>kidx   <span class="comment">; if the button was just now pressed and...</span>
  kres   <span class="opc">tab</span>       kidx, 1    <span class="comment">; if button value is one, start a note</span>
         <span class="opc">schedkwhen</span>  kres<span class="op">*</span>kcheck, 1, 20, 2, 0, 60000, kidx, kx0, ky0
  kidx   <span class="op">=</span>  kidx<span class="op">+</span>1
<span class="octrl">if</span> kidx <span class="op">&lt;</span> (kaxes<span class="op">+</span>kbuttons<span class="op">+</span>2) <span class="octrl">kgoto</span> <span class="olabel">buttons</span>

<span class="oblock">endin</span>

<span class="oblock">instr</span> 2 <span class="comment">; play a tone until the button is released</span>

kstop   <span class="opc">tab</span>  p4, 1 <span class="comment">; when this button is released, we fade out</span>
ihz     <span class="opc">init</span> <span class="opc">cpsoct</span>(((p5<span class="op">+</span>32767)<span class="op">/</span>9362)<span class="op">+</span>5) <span class="comment">; ~ 30 hz to 4khz</span>
<span class="opc">print</span> ihz
ito     <span class="opc">init</span> <span class="opc">ampdb</span>(((p6<span class="op">+</span>32767)<span class="op">/</span>2184)<span class="op">+</span>60) <span class="comment">; ~ 60 - 90 db</span>
kenv    <span class="opc">init</span> 0
kdelta  <span class="opc">init</span> ito<span class="op">/</span>(<span class="ohdr">kr</span><span class="op">*</span>10)
<span class="octrl">if</span> kstop <span class="op">==</span> 1 <span class="octrl">kgoto</span> <span class="olabel">output</span>
<span class="octrl">if</span> kdelta <span class="op">&lt;</span> 0 <span class="octrl">kgoto</span> <span class="olabel">output</span>
kdelta    <span class="op">=</span>  kdelta<span class="op">*</span><span class="op">-</span>1

<span class="olabel">output</span><span class="op">:</span>
  kenv <span class="op">=</span>  kenv<span class="op">+</span>kdelta
  kenv <span class="opc">limit</span> kenv, 0, ito
  aout <span class="opc">oscils</span> 1, ihz, 0
  aout <span class="op">=</span>  kenv<span class="op">*</span>aout
       <span class="opc">outs</span> aout, aout
<span class="octrl">if</span> kenv <span class="op">!=</span> 0 <span class="octrl">kgoto</span> <span class="olabel">noexit</span>
<span class="octrl">if</span> kdelta <span class="op">&gt;</span> 0 <span class="octrl">kgoto</span> <span class="olabel">noexit</span>
<span class="opc">turnoff</span>
<span class="olabel">noexit</span><span class="op">:</span>

<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">f</span>1  0 32    7     0     7     0         <span class="comment">; will hold the joystick data</span>

<span class="stamnt">i</span>1  0  60000

<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
      </div>
      <div class="refsect1">
        <a id="idp118204976"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Justin Glenn Smith</td>
          </tr>
          <tr>
            <td>2010</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in version 5.17.12</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="jitter2.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="jspline.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">jitter2 </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> jspline</td>
        </tr>
      </table>
    </div>
  </body>
</html>
